import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class MyCustomController extends StatefulWidget {
  final VideoPlayerController videoPlayerController;

  MyCustomController(this.videoPlayerController);

  @override
  _MyCustomControllerState createState() => _MyCustomControllerState();
}

class _MyCustomControllerState extends State<MyCustomController> {
  double _currentSliderValue = 0.0;
  bool isVideoPaused = false;
  String _formattedTime = '';
  @override
  void initState() {
    super.initState();
    widget.videoPlayerController.addListener(_updateSliderValue);
  }

  @override
  void dispose() {
    widget.videoPlayerController.removeListener(_updateSliderValue);
    super.dispose();
  }

  void _updateSliderValue() {
    final Duration currentPosition = widget.videoPlayerController.value.position;
    final Duration totalDuration = widget.videoPlayerController.value.duration;
    if (totalDuration.inSeconds > 0) {
      setState(() {
        _currentSliderValue = currentPosition.inSeconds / totalDuration.inSeconds;
        _formattedTime = _formatDuration(currentPosition);
      });
    }
  }

  void _onSliderChanged(double value) {
    final Duration totalDuration = widget.videoPlayerController.value.duration;
    widget.videoPlayerController.seekTo(Duration(seconds: (value * totalDuration.inSeconds).round()));
  }
  String _formatDuration(Duration duration) {
    if (duration == null) return '00:00:00';

    final hours = duration.inHours;
    final minutes = duration.inMinutes % 60;
    final seconds = duration.inSeconds % 60;

    String formattedTime = '${hours.remainder(60)}:${minutes.remainder(60)}:${seconds}';

    return formattedTime;
  }
  void _onTap() {
    setState(() {
      isVideoPaused = !isVideoPaused;
    });
    isVideoPaused ? widget.videoPlayerController.pause() : widget.videoPlayerController.play();
  }
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _onTap,

      child: Container(
        // width: double.infinity,
        // height: double.infinity,
        color: Colors.transparent,
        child: Stack(
          children: <Widget>[

            // 进度条位于页面底部
            Positioned(
              bottom: 3,
              left: 0,
              right: 0,
              child: Container(
                // padding: EdgeInsets.all(10.0),
                height: 25,
                child: Row(
                  children: <Widget>[

                    Expanded(
                      child: SliderTheme(
                        data: SliderTheme.of(context).copyWith(
                          // 自定义滑块（拇指）的大小
                          thumbShape: RoundSliderThumbShape(enabledThumbRadius: 4.5),
                          // 自定义轨道的尺寸
                          trackHeight: 1,

                        ),
                        child: Slider(
                          value: _currentSliderValue,
                          min: 0,
                          max: 1,
                          divisions: 300,

                          activeColor: Colors.grey
                          ,
                          //     ? Color(0xfff69f96) // 使用柔和的灰色
                          //     : Colors.white, // 设置滑块轨道和拇指的颜色
                          inactiveColor: Colors.grey,
                          // inactiveColor: Theme.of(context).textTheme.bodyText1?.color, // 设置滑块轨道的颜色
                          // thumbColor:  Theme.of(context).brightness == Brightness.light
                          //     ? Color(0xfff69f96) // 使用柔和的灰色
                          //     : Colors.white, // 设置滑块拇指的颜色
                          onChanged: _onSliderChanged,
                        ),
                      ),
                    ),

                    Text(
                      _formattedTime,
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Colors.grey

                        // color: Theme.of(context).textTheme.bodyText1?.color
                      ),
                      // _currentSliderValue != null ? '${(_currentSliderValue * 100).round()}%' : '0%'// 确保文本颜色可见
                    ),

                  ],
                ),
              ),
            ),

          ],
        ),
      ) ,
    ) ;
  }
}
